﻿using System;
using System.IO;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace RevitCommands
{
	public class ShortcutsFile
	{
		public static string GetShortcuts(string strCmdId, Document doc)
		{
			string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
			string versionNumber = RevitVersion.GetVersionNumber(doc);
			string str = folderPath + "\\Autodesk\\Revit\\Autodesk Revit " + versionNumber;
			string filename = str + "\\KeyboardShortcuts.xml";
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(filename);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("Shortcuts");
				foreach (object obj in xmlNode.ChildNodes)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					XmlElement xmlElement = xmlNode2 as XmlElement;
					if (strCmdId == xmlElement.GetAttribute("CommandId"))
					{
						return xmlElement.GetAttribute("Shortcuts");
					}
				}
			}
			catch
			{
				return null;
			}
			return null;
		}

		public static void SetKeyboardShortcuts(string versionName, string commandName, string shortcuts, string ribbonTabName, string ribbonPanelName)
		{
			string path = Environment.ExpandEnvironmentVariables("%appdata%\\Autodesk\\Revit");
			string path2 = Path.Combine(path, versionName);
			string text = Path.Combine(path2, "KeyboardShortcuts.xml");
			if (File.Exists(text))
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(text);
				ShortcutsFile.SetOneKeyboardShortcuts(xmlDocument, commandName, shortcuts, ribbonTabName, ribbonPanelName);
				xmlDocument.Save(text);
			}
		}

		public static void SetKeyboardShortcuts(string versionName, string commandName, string commandId, string shortcuts, string ribbonTabName, string ribbonPanelName)
		{
			string path = Environment.ExpandEnvironmentVariables("%appdata%\\Autodesk\\Revit");
			string path2 = Path.Combine(path, versionName);
			string text = Path.Combine(path2, "KeyboardShortcuts.xml");
			if (File.Exists(text))
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(text);
				ShortcutsFile.SetOneKeyboardShortcuts(xmlDocument, commandName, commandId, shortcuts, ribbonTabName, ribbonPanelName);
				xmlDocument.Save(text);
			}
		}

		private static void SetOneKeyboardShortcuts(XmlDocument doc, string commandName, string commandId, string shortcuts, string ribbonTabName, string ribbonPanelName)
		{
			XmlElement documentElement = doc.DocumentElement;
			string text = ribbonTabName + "&gt;" + ribbonPanelName;
			string xpath = string.Format("Shortcuts/ShortcutItem[@CommandName='{0}']", commandName);
			XmlNode xmlNode = doc.SelectSingleNode(xpath);
			if (xmlNode == null)
			{
				XmlElement xmlElement = doc.CreateElement("ShortcutItem");
				xmlElement.SetAttribute("CommandName", commandName);
				xmlElement.SetAttribute("CommandId", commandId);
				xmlElement.SetAttribute("Shortcuts", shortcuts);
				xmlElement.SetAttribute("Paths", text);
				documentElement.AppendChild(xmlElement);
				return;
			}
			XmlElement xmlElement2 = xmlNode as XmlElement;
			if (!xmlElement2.HasAttribute("Shortcuts"))
			{
				xmlElement2.SetAttribute("Shortcuts", shortcuts);
			}
			else if (xmlElement2.GetAttribute("Shortcuts").CompareTo(shortcuts) != 0)
			{
				xmlElement2.SetAttribute("Shortcuts", shortcuts);
			}
			if (!xmlElement2.HasAttribute("CommandId"))
			{
				xmlElement2.SetAttribute("CommandId", commandId);
			}
			else if (xmlElement2.GetAttribute("CommandId").CompareTo(commandId) != 0)
			{
				xmlElement2.SetAttribute("CommandId", commandId);
			}
			if (!xmlElement2.HasAttribute("Paths"))
			{
				xmlElement2.SetAttribute("Paths", text);
				return;
			}
			if (xmlElement2.GetAttribute("Paths").CompareTo(text) != 0)
			{
				xmlElement2.SetAttribute("Paths", text);
			}
		}

		private static void SetOneKeyboardShortcuts(XmlDocument doc, string commandName, string shortcuts, string ribbonTabName, string ribbonPanelName)
		{
			XmlElement documentElement = doc.DocumentElement;
			string text = string.Concat(new string[]
			{
				"CustomCtrl_%CustomCtrl_%",
				ribbonTabName,
				"%",
				ribbonPanelName,
				"%",
				commandName
			});
			string text2 = ribbonTabName + "&gt;" + ribbonPanelName;
			string xpath = string.Format("Shortcuts/ShortcutItem[@CommandName='{0}']", commandName);
			XmlNode xmlNode = doc.SelectSingleNode(xpath);
			if (xmlNode == null)
			{
				XmlElement xmlElement = doc.CreateElement("ShortcutItem");
				xmlElement.SetAttribute("CommandName", commandName);
				xmlElement.SetAttribute("CommandId", text);
				xmlElement.SetAttribute("Shortcuts", shortcuts);
				xmlElement.SetAttribute("Paths", text2);
				documentElement.AppendChild(xmlElement);
				return;
			}
			XmlElement xmlElement2 = xmlNode as XmlElement;
			if (!xmlElement2.HasAttribute("Shortcuts"))
			{
				xmlElement2.SetAttribute("Shortcuts", shortcuts);
			}
			if (!xmlElement2.HasAttribute("CommandId"))
			{
				xmlElement2.SetAttribute("CommandId", text);
			}
			else if (xmlElement2.GetAttribute("CommandId").CompareTo(text) != 0)
			{
				xmlElement2.SetAttribute("CommandId", text);
			}
			if (!xmlElement2.HasAttribute("Paths"))
			{
				xmlElement2.SetAttribute("Paths", text2);
				return;
			}
			if (xmlElement2.GetAttribute("Paths").CompareTo(text2) != 0)
			{
				xmlElement2.SetAttribute("Paths", text2);
			}
		}
	}
}
